<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\Banners;
use App\Model\Home;
use App\Model\OperationLogs;
use App\Model\TemplateBlock;
use App\Model\Ad;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BannerController extends BaseController
{
    public function __construct(Request $request, Banners $banners)
    {
        parent::__construct($request);
        $this->model = $banners;
    }

    /**
     * 后台获取banner列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author 陈凯强
     */
    public function lists(Request $request)
    {
        $results = $this->model->getList($request);
        return $this->toJson(self::SUCCESS, $results);
    }
    /**
     * 新增广告信息
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     * @author 陈凯强
     * @chagetime 2023年07月17日18:59:08
     */
    public function addAdv(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required|max:50|string', //banner名称
            'sort' => 'required', //排序规则
            'mincharm' => 'required', //最小魅力值
            'maxcharm' => 'required', //最大魅力值
            'minrich' => 'required', //最小财富等级
            'maxrich' => 'required', //最大财富等级
            'type' => 'required|integer|in:0,1,2,3,4,5,6,7,10,11,12,13,14',
            'start_timestamp' => 'nullable|date_format:Y-m-d H:i:s',
            'end_timestamp' => 'nullable|date_format:Y-m-d H:i:s',
        ],
            [
                'name.required' => '名称不可为空',
                'name.string' => '名称必须为字符串',
                'img.required' => '图片不可为空',
                'type.required' => '广告位置不可为空',
                'sort.required' => '广告顺序不可为空'
            ]);

        if ($validator->fails()) {
            return $this->toJson(self::PARAM_ERROR, [], $validator->errors()->all()[0]);
        }

        //接收参数
        $data = $request->all();

        $adRes = $this->model->createData($data);
        if ($adRes['code'] !== 0) {
            return $this->toJson(self::ERROR,[],$adRes['msg']);
        }
        $data['id'] = $adRes['data']['id'];
        $data['status'] = intval($data['status']);
        if (!isset($data['start_timestamp'])) $data['start_timestamp'] = time();
        if (!isset($data['end_timestamp'])) $data['end_timestamp'] = 0;
        if (!isset($data['desc'])) $data['desc'] = '';
        OperationLogs::createData($request, OperationLogs::BANNER, '新增广告');
        return $this->toJson(self::SUCCESS, $data);
    }

    /**
     * 更改banner信息
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @author 陈凯强
     */
    public function updateAd(Request $request)
    {
        $adId = $request->post('id');
        $validator = Validator::make($request->all(),[
            'id' =>'required',
            'name' => 'required|max:50|string', //banner名称
            'sort' => 'required', //排序规则
            'mincharm' => 'required', //最小魅力值
            'maxcharm' => 'required', //最大魅力值
            'minrich' => 'required', //最小财富等级
            'maxrich' => 'required', //最大财富等级
            'type' => 'required|integer|in:0,1,2,3,4,5,6,7,10,11,12,13,14',
            'start_timestamp' => 'nullable|date_format:Y-m-d H:i:s',
            'end_timestamp' => 'nullable|date_format:Y-m-d H:i:s',
        ],
            [
                'id.required' => 'id不能为空',
                'name.required' => '名称不可为空',
                'name.string' => '名称必须为字符串',
                'img.required' => '图片不可为空',
                'type.required' => '广告位置不可为空',
                'sort.required' => '广告顺序不可为空'
            ]);
        if ($validator->fails()) {
            return $this->toJson(self::PARAM_ERROR, [], $validator->errors()->all()[0]);
        }
        $data = $request->all();
        $data['id'] = $adId;
        $adRes = $this->model->updateData($data);
        if (!empty($data['start_timestamp'])) {
            $data['start_timestamp'] = strtotime($data['start_timestamp']);
        }else{
            $data['start_timestamp'] = time();
        }
        if ($adRes['code'] !== 0) {
            return $this->toJson(self::ERROR, [],$adRes['msg']);
        }
        OperationLogs::createData($request, OperationLogs::BANNER, '编辑广告',$request->input('id'));
        return $this->toJson(self::SUCCESS, $data);
    }

    /**
     * 删除广告
     *
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * @author 陈凯强
     */
    public function destroyAd(Request $request)
    {
        $delId = $request->post('id');
        if (empty($delId))
        {
            return $this->toJson(self::ERROR,[],"缺少必要参数");
        }
        $model = $this->model->find($delId);
        if (empty($model))
        {
            return $this->toJson(self::ERROR,[],"数据不存在");
        }
        $gift = $this->model->removeData(['id' => $delId]);
        if ($gift['code'] !== 0) {
            return $this->toJson(self::ERROR, $gift);
        }
        OperationLogs::createData($request, OperationLogs::BANNER, '删除广告', $delId);
        return $this->toJson();
    }

}


